﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BillNavigationViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="BillNavigationViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Bill.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    using Common.BaseClasses;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common;
    using Hisaab.Common;

    using Microsoft.Practices.Prism;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="BillNavigationViewModel"/> type.
    /// </summary>
    public class BillNavigationViewModel : NavigationViewModel
    {
        #region Data Members

        /// <summary>
        /// Member variable _billManager
        /// </summary>
        private readonly IBillManager _billManager;

        /// <summary>
        /// Member variable _bills
        /// </summary>
        private ObservableCollection<Model.Bill> _bills;

        /// <summary>
        /// Member variable _selectedBill
        /// </summary>
        private Model.Bill _selectedBill;

        /// <summary>
        /// Member variable _refreshCommand
        /// </summary>
        private ICommand _refreshCommand;

        #endregion Data Members

        /// <summary>
        /// Initializes a new instance of the <see cref="BillNavigationViewModel"/> class.
        /// </summary>
        public BillNavigationViewModel()
            : this(ServiceLocator.Current.GetInstance<IBillManager>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BillNavigationViewModel"/> class.
        /// </summary>
        /// <param name="billManager">The target manager.</param>
        public BillNavigationViewModel(IBillManager billManager)
        {
            Contract.Requires<ArgumentNullException>(billManager != null);
            _billManager = billManager;

            Title = "Bills";
            Image = new BitmapImage(
                        new Uri(
                        "Pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/32/Bill.png",
                        UriKind.Absolute));

            ImageSmall =
                        new BitmapImage(
                        new Uri(
                        "Pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/16/Bill.png",
                        UriKind.Absolute));

            GlobalCommands.Refresh.RegisterCommand(RefreshCommand);
            RefreshList();
        }

        /// <summary>
        /// Gets or sets the bills.
        /// </summary>
        /// <value>The bills.</value>
        public ObservableCollection<Model.Bill> Bills
        {
            get
            {
                return _bills;
            }

            set
            {
                RaisePropertyChangingEvent("Bills");
                _bills = value;
                RaisePropertyChangedEvent("Bills");
            }
        }

        /// <summary>
        /// Gets the refresh command.
        /// </summary>
        /// <value>
        /// The refresh command.
        /// </value>
        public ICommand RefreshCommand
        {
            get
            {
                if (_refreshCommand == null)
                {
                    _refreshCommand = new DelegateCommand(RefreshList);
                }

                return _refreshCommand;
            }
        }

        /// <summary>
        /// Gets or sets the selected bill.
        /// </summary>
        /// <value>The selected bill.</value>
        public Model.Bill SelectedBill
        {
            get
            {
                return _selectedBill;
            }

            set
            {
                RaisePropertyChangingEvent("SelectedBill");
                _selectedBill = value;
                RaisePropertyChangedEvent("SelectedBill");
                OnTargetChanged();
            }
        }

        /// <summary>
        /// Called when target changed.
        /// </summary>
        public void OnTargetChanged()
        {
            if (SelectedBill != null)
            {
                // Initialize
                var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

                // Show Workspace
                var query = new UriQuery { { "ID", SelectedBill.Id.ToString(CultureInfo.InvariantCulture) } };
                var dashboardUri = new Uri(BillModule.BillViewName + query, UriKind.RelativeOrAbsolute);
                regionManager.RequestNavigate(Regions.WorkspaceRegion, dashboardUri);

                LastNavigatedUri = dashboardUri;
            }
        }

        /// <summary>
        /// Refreshes the list.
        /// </summary>
        public void RefreshList()
        {
            Bills = new ObservableCollection<Model.Bill>(_billManager.List().Select(x => x.Map<Business.Entities.Bill, Model.Bill>()));
        }
    }
}